package com.imau.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.imau.bean.Teacher;
import com.imau.service.TeacherService;
import com.imau.service.impl.TeacherServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/teacher/*")
public class TeacherConterllor extends Baseconterllor {

    private TeacherService teacherService = new TeacherServiceImpl();


    /**
     * 添加教师
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //前端JSON对象
        String jsonString = request.getReader().readLine();

        //转成对象
        Teacher teacher = JSON.parseObject(jsonString, Teacher.class);

        //3.保存
        Teacher entiry = teacherService.save(teacher);

        //4.返回响应

        this.writeResult(response, entiry == null ? false : true);


    }


    /**
     * 删除
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void removeById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //前端JSON对象
        String tid = request.getParameter("tid");


        //删除
        int row = teacherService.deleteById(tid);
        //4.返回响应

        this.writeResult(response, row > 0 ? true : false);


    }


    /**
     * 修改
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //前端JSON对象
        String jsonString = request.getReader().readLine();

        //转成对象
        Teacher teacher = JSON.parseObject(jsonString, Teacher.class);

        //3.保存
        int row = teacherService.updateTeacher(teacher);

        //4.返回响应

        this.writeResult(response, row > 0 ? true : false);


    }


    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //前端JSON对象
        String jsonString = request.getReader().readLine();

        //转成对象
        Teacher teacher = JSON.parseObject(jsonString, Teacher.class);

        String currentPage = request.getParameter("currentPage");

        String pageSize = request.getParameter("pageSize");

        //3.保存

        PageInfo<Teacher> teachers = teacherService.queryListByPagte(teacher, Integer.valueOf(currentPage), Integer.valueOf(pageSize));

        //4.返回响应
        writeResultAndData(response, teachers);


    }

    /**
     * 查询
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */


    protected void getInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String tid = request.getParameter("tid");


        Teacher teacher = teacherService.findById(tid);

        //4.返回响应
        writeResultAndData(response, teacher);


    }

}
